package com.hl.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.domain.Publicity;
import com.hl.domain.User;
import com.hl.mapper.PublicityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service

public class PublicityService {

    @Autowired
    private PublicityMapper publicityMapper;



    //    增加
    public int addNotice(Publicity publicity) {
        return publicityMapper.insert(publicity);
    }

    //    删除
    public int deleteNotice(Long id) {
        return publicityMapper.deleteByPrimaryKey(id);
    }
    //    修改
    public int updateNotice(Publicity publicity) {
        return publicityMapper.updateByPrimaryKeySelective(publicity);
    }

    //    获取全部数据
    public HashMap<String, Object> fetchAll(int page, int size) {
        //设置分页
        PageHelper.startPage(page, size);
        //查询全部数据
        List<Publicity> list = publicityMapper.queryAll();
        PageInfo<Publicity> pageInfo = new PageInfo<Publicity>(list);
        //获取总数
        int total = (int) pageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("list", list);
        return map;
    }

    //    根据名字、时间模糊搜索
    public HashMap<String, Object> search(String name, LocalDateTime begintime, LocalDateTime endtime, int page, int size) {
        PageHelper.startPage(page, size);
        List<Publicity> list = publicityMapper.queryByTitleAndTime(name, begintime, endtime);
        PageInfo<Publicity> pageInfo = new PageInfo<>(list);
//        获取总数
        Long total = pageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<>();
//        返回值
//        页码信息
        map.put("list", list);
        map.put("total", total);
        return map;

    }

//    查询所有通知公共内容
    public List<Publicity> selectAll(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        return publicityMapper.selectAll();
    }
}
